package com.qinggeng.project.api.controller;

import com.google.common.collect.Maps;
import com.qinggeng.common.constant.Constants;
import com.qinggeng.common.enums.CheckStatusEnum;
import com.qinggeng.common.enums.JobUserTypeEnum;
import com.qinggeng.common.enums.ShowTypeEnum;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.project.api.security.annotation.LoginRequired;
import com.qinggeng.project.api.security.utils.ApiUtils;
import com.qinggeng.project.system.domain.Company;
import com.qinggeng.project.system.domain.JobUser;
import com.qinggeng.project.system.service.ICompanyService;
import com.qinggeng.project.system.service.IJobUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author smallfish
 *
 */
@Api(tags = "公司模块")
@RestController
@CrossOrigin
@RequestMapping("${apiPath}/company")
public class ApiCompanyController extends BaseController {

    @Autowired
    private ICompanyService companyService;
    @Autowired
    private IJobUserService jobUserService;

    @ApiOperation("公司列表")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记",required = true)
    })
    @LoginRequired()
    public AjaxResult companyList(){
        startPage();
        return AjaxResult.success(companyService.listCompanyByJobUserId());
    }

    @ApiOperation("编辑公司")
    @PostMapping("/edit")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记", required = true)
    })
    @LoginRequired()
    public AjaxResult editCompany(Company company){
        Long jobUserId=jobUserService.getJobUserId();
        //新增公司
        if(null==company.getId()){
            company.setJobUserId(jobUserId);
            return toAjax(companyService.insertCompany(company));
        }
        //更新公司
        if(null==company.getJobUserId()||company.getJobUserId().compareTo(jobUserId)!=0){
            return AjaxResult.error("没有权限修改！",ShowTypeEnum.用户.getValue());
        }
        return toAjax(companyService.updateCompany(company));
    }

    @ApiOperation("删除公司")
    @PostMapping("/delete")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "Long",dataType = "query",name = "id",value = "id",required = true),
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记", required = true)
    })
    @LoginRequired()
    public AjaxResult deleteCompany(Long id){
        return toAjax(companyService.deleteCompanyById(id));
    }

    @ApiOperation("公司详情")
    @GetMapping("/info")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "Long",dataType = "query",name = "id",value = "id",required = true),
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记")
    })
    @LoginRequired(must = false)
    public AjaxResult companyInfo(Long id){
        Company company=companyService.getCompanyById(id);
        if(null==company.getStatus()||!company.getStatus().equals(CheckStatusEnum.已通过.getValue())){
            return AjaxResult.error("等待审核通过",ShowTypeEnum.用户.getValue());
        }
        return AjaxResult.success(company);
    }

    @ApiOperation("设为默认")
    @PostMapping("/default")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "Long",dataType = "query",name = "id",value = "id",required = true),
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记")
    })
    @LoginRequired()
    public AjaxResult setDefault(Long id){
        JobUser jobUser=jobUserService.getJobUser();
        Company company=companyService.getCompanyByIdAndJobUserId(jobUser,id);
        if(null==company.getStatus()||!company.getStatus().equals(CheckStatusEnum.已通过.getValue())){
            return AjaxResult.error("等待审核通过",ShowTypeEnum.用户.getValue());
        }
        return toAjax(companyService.setDefaultCompany(jobUser,id));
    }



    @ApiOperation("检查是否拥有公司")
    @GetMapping("/exists")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记", required = true)
    })
    @LoginRequired()
    public AjaxResult checkCompany(){
        JobUser jobUser=jobUserService.getJobUser();
        Map<String,Object> data= Maps.newHashMap();
        data.put("exists",companyService.checkCompanyIsExists(jobUser.getJobUserId()));
        data.put("num",companyService.getCompanyNumByJobUserId(jobUser.getJobUserId(),null));
        data.put("company",companyService.getDefaultCompany(jobUser));
        return AjaxResult.success(data);
    }


}
